package com.wyvoid.core.tag;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.TagSupport;
import java.lang.reflect.Field;
import java.lang.reflect.AccessibleObject;
import java.lang.reflect.Modifier;
import java.util.Map;
import java.util.HashMap;

/**
 * 向JSP暴露JSTL可以直接使用的常量的TAG
 * User: cjp
 * Date: 2006-11-6
 * Time: 16:08:42
 */
public class ConstantTag extends TagSupport {
    private static final long serialVersionUID = 3258417209566116146L;

    /**
     * 要暴露的常量的CLASS的全名称,如com.mysite.Constant<br>
     * 如果Constant中有innerClass,可以这样引用:com.mysite.Constant$Blog,其中Blog是Constant<br>
     * 的innerClass
     */
    public String clazz;

    /**
     * 常量暴露的范围<br>
     * page,request,session,application
     */
    protected String scope = null;

    /**
     * 只暴露常量类中的一个常量的名称
     */
    protected String var = null;

    public int doStartTag() throws JspException {
        Class c;
        int toScope = PageContext.PAGE_SCOPE;

        if (scope != null) {
            toScope = getScope(scope);
        }

        try {
            c = Class.forName(clazz);
        } catch (ClassNotFoundException cnf) {
            throw new JspException(cnf.getMessage());
        }

        try {
            // 如果var==null,则暴露所有的常量;反之只暴露变量名为var的变量
            if (var == null) {
                Field[] fields = c.getDeclaredFields();

                AccessibleObject.setAccessible(fields, true);

                for (Field field : fields) {
                    if (Modifier.toString(field.getModifiers()).indexOf("public") != -1) {
                        pageContext.setAttribute(field.getName(),
                                field.get(this), toScope);
                    }
                }
            } else {
                try {
                    Object value = c.getField(var).get(this);
                    pageContext.setAttribute(c.getField(var).getName(), value,
                            toScope);
                } catch (NoSuchFieldException nsf) {
                    throw new JspException(nsf);
                }
            }
        } catch (IllegalAccessException iae) {
            throw new JspException(iae);
        }

        return (SKIP_BODY);
    }

    /**
     * @param clazz 常量class
     * @ jsp.attribute
     */
    public void setClassName(String clazz) {
        this.clazz = clazz;
    }

    public String getClassName() {
        return this.clazz;
    }

    /**
     * @param scope
     * page,request,session,application
     * @ jsp.attribute
     */
    public void setScope(String scope) {
        this.scope = scope;
    }

    public String getScope() {
        return (this.scope);
    }

    /**
     * @param var var
     * @ jsp.attribute
     */
    public void setVar(String var) {
        this.var = var;
    }

    public String getVar() {
        return (this.var);
    }

    /**
     * 释放所有引用的资源
     */
    public void release() {
        super.release();
        clazz = null;
        scope = null;
    }

    // ~========== From Struts' TagUtils class =====================

    /**
     * Maps lowercase JSP scope names to their PageContext integer constant
     * values.
     */
    private static final Map scopes = new HashMap();

    /**
     * Initialize the scope names map and the encode variable with the
     * Java 1.4 method if available.
     */
    static {
        scopes.put("page", PageContext.PAGE_SCOPE);
        scopes.put("request", PageContext.REQUEST_SCOPE);
        scopes.put("session", PageContext.SESSION_SCOPE);
        scopes.put("application", PageContext.APPLICATION_SCOPE);
    }

    /**
     * Converts the scope name into its corresponding PageContext constant value.
     *
     * @param scopeName Can be "page", "request", "session", or "application" in any
     *                  case.
     * @return The constant representing the scope (ie. PageContext.REQUEST_SCOPE).
     * @throws javax.servlet.jsp.JspException if the scopeName is not a valid name.
     */
    public int getScope(String scopeName) throws JspException {
        Integer scope = (Integer) scopes.get(scopeName.toLowerCase());

        if (scope == null) {
            throw new JspException("Scope '" + scopeName + "' not a valid option");
        }

        return scope;
    }


    public String getClazz() {
        return clazz;
    }

    public void setClazz(String clazz) {
        this.clazz = clazz;
    }
}
